<template>
	<l-page :page.sync="queryParams.pageNum" :page-size.sync="queryParams.pageSize" :total="total" @getList="getList">
		<div slot="header">
			<l-back class="mb-[24px]" />
			<el-form ref="queryForm" :model="queryParams" :inline="true" label-width="90px">
				<el-form-item label="商品ID:" prop="id"> <el-input v-model.trim="queryParams.id" maxlength="6" show-word-limit placeholder="请输入商品ID" clearable /></el-form-item>
				<el-form-item label="商品名称:" prop="name">
					<el-input v-model.trim="queryParams.name" clearable placeholder="请输入商品名称" maxlength="11" show-word-limit />
				</el-form-item>
				<el-form-item label="店铺名称:" prop="storeName">
					<el-input v-model.trim="queryParams.storeName" placeholder="请输入店铺名称" clearable />
				</el-form-item>
				<el-form-item label="商品分类:" prop="cate">
					<el-cascader ref="category" v-model="queryParams.cate" :props="props" placeholder="请选择商品分类" @change="change"></el-cascader>
				</el-form-item>

				<start-end-time ref="child" :get-time="getTime" title="申请时间:"></start-end-time>
			</el-form>
		</div>
		<template slot="options">
			<l-search @search="handleQuery" @refresh="resetQuery" />
		</template>
		<el-table ref="goodsDown" v-loading="loading" stripe :data="goodsList" @sort-change="handleSortChange">
			<el-table-column :formatter="emptyToString" align="center" label="序号" type="index" width="55" />
			<el-table-column :formatter="emptyToString" align="center" label="商家ID" prop="storeId" min-width="120"> </el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="手机号码" prop="storePhone" min-width="120"> </el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="商家名称" min-width="120" prop="name" show-overflow-tooltip>
				<template slot-scope="scope">
					<div class="goods-name-style">
						{{ scope.row.name }}
					</div>
				</template>
			</el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="商品ID" prop="id" min-width="120"> </el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="商品名称" min-width="120" prop="name" show-overflow-tooltip>
				<template slot-scope="scope">
					<div class="goods-name-style">
						{{ scope.row.name }}
					</div>
				</template>
			</el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="商品图片" min-width="120">
				<template slot-scope="scope">
					<el-image style="width: 48px; height: 48px" :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]"></el-image>
				</template>
			</el-table-column>

			<el-table-column :formatter="emptyToString" align="center" label="商品类型" min-width="120">
				<template slot-scope="scope">
					{{ scope.row.topCategoryName + ' > ' + scope.row.categoryName }}
				</template>
			</el-table-column>
			<!-- <el-table-column :formatter="emptyToString" align="center" label="供应商名称" prop="supplierName" min-width="156"> </el-table-column> -->
			<!-- <el-table-column :formatter="emptyToString" align="center" label="商品分类" min-width="120">
				<template slot-scope="scope">
					<div>{{ scope.row.topCategoryName }}>{{ scope.row.categoryName }}</div>
				</template>
			</el-table-column> -->
			<el-table-column :formatter="emptyToString" align="center" label="商品价格" prop="retailPrice" min-width="120" sortable />
			<el-table-column :formatter="emptyToString" align="center" label="供应价格" prop="supplyPrice" min-width="120" />
			<el-table-column :formatter="emptyToString" align="center" label="申请时间" min-width="200" prop="createTime">
				<template slot-scope="scope">
					<div class="w-full flex flex-col items-center">
						<span>{{ dayjs(scope.row.createTime).format('YYYY-MM-DD') }}</span>
						<span>{{ dayjs(scope.row.createTime).format('HH:mm:ss') }}</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column :formatter="emptyToString" align="center" label="操作" width="100" fixed="right">
				<template slot-scope="scope">
					<div class="table-action flex justify-center">
						<el-button type="primary" @click="handleUpdate(scope.row)">审核 </el-button>
					</div>
				</template>
			</el-table-column>
		</el-table>
	</l-page>
</template>

<script>
import { delGoods, addGoods, updateGoods, exportGoods, updateGoodsList, getAuditGoods, getAuditStoreGoods, auditGoods } from '@/api/mall/goods';
import { listChild, listParent, listStoreParent } from '@/api/mall/category';
import { isArray } from '@/utils/validate';
import tableMixins from '@/mixins/table';
export default {
	name: 'Audit',
	components: {},
	mixins: [tableMixins],
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 商品基本信息表格数据
			goodsList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 搜索参数
			queryParams: {
				current: 1,
				size: 10,
				id: null,
				cate: [],
				supplierName: null,
				auditStatus: null,
				categoryId: null, //二级分类
				topCategoryId: null, //一级分类
				name: null,
			},
			info: {},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				retailPrice: [{ required: true, message: '当前价格不能为空', trigger: 'blur' }],
				name: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
			},

			detailDialogVisible: false,
			goodsTypeList: [],
			show: false,
			retailPrice: '',
			auditOpinion: '',
			props: {
				lazy: true,
				lazyLoad(node, resolve) {
					const { level, value } = node;
					if (level === 0) {
						// listParent().then((response) => {
						listStoreParent(value).then((response) => {
							const { data } = response;
							const nodes = data.map((item) => ({
								value: item.id,
								label: item.name,
								leaf: false,
							}));
							resolve(nodes);
						});
					} else {
						listChild(value).then((response) => {
							const { data } = response;
							const nodes = data.map((item) => ({
								value: item.id,
								label: item.name,
								leaf: true,
							}));
							resolve(nodes);
						});
					}
				},
			},
		};
	},

	created() {
		this.getList();
	},
	methods: {
		change(value) {
			this.queryParams.categoryId = value[value.length - 1];
			this.queryParams.topCategoryId = value[0]; //一级分类
		},
		getTime(startTime, endTime) {
			this.queryParams.startTime = startTime;
			this.queryParams.endTime = endTime;
		},
		close() {
			this.auditOpinion = '';
			this.retailPrice = '';
		},
		auditsupplier(type) {
			if (type == 1 && !this.retailPrice) {
				this.$message.error('请输入当前价格!');
				return;
			}
			auditGoods({
				auditOpinion: this.auditOpinion,
				id: this.goodsId,
				retailPrice: this.retailPrice,
				status: type,
			}).then((res) => {
				this.$message.success('操作成功!');
				this.show = false;
				this.getList();
			});
		},
		getList() {
			this.loading = true;
			getAuditStoreGoods(this.queryParams)
				.then((response) => {
					this.goodsList = response.data.records;
					this.total = response.data.total;
				})
				.finally(() => {
					this.loading = false;
				});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				goodsSn: null,
				name: null,
				categoryId: null,
				brandId: null,
				gallery: null,
				keywords: null,
				brief: null,
				isOnSale: null,
				sortOrder: null,
				picUrl: null,
				shareUrl: null,
				isNew: null,
				isHot: null,
				unit: null,
				counterPrice: null,
				retailPrice: null,
				detail: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.current = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.$refs.child.resetTime();
			this.queryParams.categoryId = null;
			this.resetForm('queryForm');
			this.handleQuery();
		},
		handleUpdate(row) {
			console.log('row', row);
			// this.$router.push(`/goods/details?id=${row.id}&type=audit`);
			this.$router.push('/goods/addStore?id=' + row.id + '&storeId=' + row.storeId + '&pageNum=' + this.queryParams.pageNum + '&type=isAudit');
			// this.$router.push('/goods/addStore');
			// this.goodsId  = row.id
			//  this.show=true
		},

		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateGoods(this.form).then(() => {
							this.msgSuccess('编辑成功');
							this.open = false;
							this.getList();
						});
					} else {
						addGoods(this.form).then(() => {
							this.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			let ids = row.id || this.ids;
			if (!isArray(ids)) {
				ids = [ids];
			}

			this.$confirm('是否确认删除商品基本信息编号为"' + ids + '"的数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delGoods(ids);
				})
				.then(() => {
					this.getList();
					this.msgSuccess('删除成功');
				});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有商品基本信息数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return exportGoods(queryParams);
				})
				.then((response) => {
					this.download(response.msg);
				});
		},
		showDetail(detail) {
			this.goodsDetail = detail;
			this.detailDialogVisible = true;
		},
		handleCategoryChange(value) {
			this.queryParams.categoryId = value[value.length - 1];
		},
		batchPutAway(row) {
			let currentSelect = this.$refs.goodsDown.selection;
			let itemArr = [];

			for (let i = 0; i < currentSelect.length; i++) {
				itemArr.push(currentSelect[i]);
				currentSelect[i].isOnSale = 1;
				console.log(currentSelect[i]);
			}
			// const data = {
			//   data: itemArr,
			// }
			updateGoodsList(itemArr).then((res) => {
				this.$message.success('取消成功!');
				this.getGoodsList();
			});
		},
		batchSoldOut(row) {
			let currentSelect = this.$refs.goodsDown.selection;
			let itemArr = [];

			for (let i = 0; i < currentSelect.length; i++) {
				itemArr.push(currentSelect[i]);
				currentSelect[i].isOnSale = 2;
				console.log(currentSelect[i]);
			}
			// const data = {
			//   data: itemArr,
			// }
			// console.log(data)
			updateGoodsList(itemArr).then((res) => {
				this.$message.success('取消成功!');
				this.getGoodsList();
			});
		},
	},
};
</script>
<style scoped lang="scss">
.btn-box {
	display: flex;
	justify-content: center;
	margin-top: 10px;
	.btn {
		padding: 0;
		font-size: 18px;
		width: 236px;
		height: 60px;
		opacity: 1;
		border-radius: 4px;
		line-height: 60px;
	}
}
.content {
	.content-info {
		.image__preview {
			display: flex;
			justify-content: center;
			.image {
				width: 329px;
				height: 162px;
				margin: 0 10px;
				display: block;
				border-radius: 8px;
			}
		}
		.info {
			display: flex;
			margin-bottom: 25px;

			> div {
				width: 50%;
				height: 40px;
				line-height: 40px;
				display: flex;
				color: #333333;
				justify-content: center;
				.label {
					width: 85px;
					font-size: 12px;
				}
				.text {
					width: 225px;
					//background: #F7F7F7;
					font-size: 14px;
					box-sizing: border-box;
					padding-left: 12px;
				}
			}
		}
		.info-plus {
			> div {
				width: 100%;
				.text {
					width: 593px;
				}
			}
		}
	}
}
</style>
